// jquery 处理事件
/*function chooseResOrWork(){
 $("#workOrResDialog") = true;
 //onclick="chooseResOrWork()"
 }*/

//  ------------ 测试产生虚拟数据 -------------------


//  ------------ 赋值 ------------------------
new Vue({
    el: '#app',
    data: function () {
        return {
            activeName: 'first',
            workCenterSearchDialog: false,
            workOrResDialog: false,
            newAddDataDialog: false,
            newAddData: [],
            selectionList: [],
            resourceOrWorkcenterData: [],
            workOrResDataDialog: [{
                name: "",
                description: ""
            }],
            resourceWorkCenterForm: {
                sequence: '',
                type: 'RESOURCE',
                name: ''
            },
            workCenterForm: {
                handle: '',
                plant: '',
                workCenter: '',
                description: '',
                statusFk: '101',
                canBeReleasedTo: '',
                wcType: 'ASSEMBLING',
                assignmentEnforcement: 'NONE',
                wcCategory: 'NONE'
            },
            statusList: [{
                label: "已启用",
                value: "101"
            }, {
                label: "已禁用",
                value: "102"
            }],
            catList: [{
                label: "无",
                value: "NONE"
            }, {
                label: "单元",
                value: "UNIT"
            }, {
                label: "单元组",
                value: "UNIT_GROUP"
            }, {
                label: "生产线",
                value: "PRODUCTION_LINE"
            }, {
                label: "生产线组",
                value: "PRODUCTION_LINE_GROUP"
            }, {
                label: "建筑物",
                value: "BUILDING"
            }],
            typeList: [{
                label: "装配",
                value: "ASSEMBLING"
            }, {
                label: "生产",
                value: "PRODUCTION"
            }, {
                label: "调度",
                value: "DISPATCH"
            }, {
                label: "装运",
                value: "SHIPMENT"
            }, {
                label: "客户",
                value: "CUSTOMER"
            }, {
                label: "生产控制",
                value: "PRODUCTION_CONTROL"
            }, {
                label: "记录控制",
                value: "RECORD_CONTROL"
            }, {
                label: "转移",
                value: "TRANSFER"
            }],
            assignList: [{
                label: "无",
                value: "NONE"
            }, {
                label: "警告",
                value: "WARNING"
            }, {
                label: "强制",
                value: "ENFORCED"
            }],
            selectType: [{
                label: "工作中心",
                value: "WORK_CENTER"
            }, {
                label: "资源",
                value: "RESOURCE"
            }],
            value: '',
            rules: {
                workCenter: [
                    {required: true, message: '请输入工作中心名称', trigger: 'blur'}
                ]
            },
            workCenterDataDialog: []
        }
    },
    methods: {
        chooseResOrWork(){
            var me = this, _form = me.resourceWorkCenterForm;
            var typeV = _form.type;
            var inputValue = _form.name;
            var param = {
                serviceName: "exposeWorkCenterService",
                serviceMethodName: "workCenterOrResourceSelect",
                plant: "TEST",
                type: typeV,
                name: inputValue
            };

            function callBack(data) {
                if (data.resultCode === "S") {
                    var resultList = data.result;
                    if (resultList.length > 0) {
                        me.workOrResDataDialog = resultList;
                        me.workOrResDialog = true;
                    }
                } else {
                    me.$notify.error({
                        title: '查询出错！',
                        message: data.resultMsg
                    });
                }
            };
            me.ajaxGetRequest(param, callBack);
        },
        addNewItem(){
            var hiddenHandle = $("#workCenterHandleHidden").val();
            if (hiddenHandle === "") {
                this.$notify.info({
                    title: '消息',
                    message: '请先检索!'
                });
            } else {
                this.newAddDataDialog = true;
                var mtableData = this.$refs.resOrWorTabData.data;
                var seq = 10;
                if (mtableData.length > 0) {
                    seq = mtableData[mtableData.length - 1].sequence + 10;
                }
                this.resourceWorkCenterForm.sequence = seq;
            }
        },
        queryWorkCenter(){
            var me = this, _form = me.workCenterForm;
            var _hiddenHandle = document.getElementById("workCenterHandleHidden").value;
            var param = {
                serviceName: "exposeWorkCenterService",
                serviceMethodName: "selectOneworkCenterByHandle",
                handle: _hiddenHandle
            };

            function callBack(data) {
                if (data.resultCode === "S") {
                    if (data.result.workCenterList.length > 0) {
                        var obj = data.result.workCenterList[0];
                        _form.workCenter = obj.workCenter;
                        _form.description = obj.description;
                        _form.statusFk = obj.statusFk;
                        _form.wcCategory = obj.wcCategory;
                        _form.wcType = obj.wcType;
                        _form.assignmentEnforcement = obj.assignmentEnforcement;
                        _form.canBeReleasedTo = obj.canBeReleasedTo === "Y" ? true : false;
                        me.resourceOrWorkcenterData = data.result.workResList;
                    }
                } else {
                    me.$notify.error({
                        title: '查询出错！',
                        message: data.resultMsg
                    });
                }
            };
            me.ajaxGetRequest(param, callBack);
        },
        workCenterSearch(){
            var me = this, _form = me.workCenterForm;
            var _workCenter = _form.workCenter;
            var param = {
                serviceName: "exposeWorkCenterService",
                serviceMethodName: "selectByWorkCenter",
                plant: "TEST",
                workCenter: _workCenter
            };

            function callBack(data) {
                if (data.resultCode === "S") {
                    me.workCenterDataDialog = data.result;
                    me.workCenterSearchDialog = true;
                } else {
                    me.$notify.error({
                        title: '查询出错！',
                        message: data.resultMsg
                    });
                }
            };
            me.ajaxGetRequest(param, callBack);
        },
        saveWorkCenter(formName){
            var me = this, _form = me.workCenterForm;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    var _form = me.workCenterForm;
                    var param = {
                        serviceName: "exposeWorkCenterService",
                        serviceMethodName: "addOrUpdateWorkCenter",
                        plant: "TEST",
                        workCenter: _form.workCenter,
                        description: _form.description,
                        statusFk: _form.statusFk,
                        canBeReleasedTo: _form.canBeReleasedTo === true ? "Y" : "N",
                        wcCategory: _form.wcCategory,
                        wcType: _form.wcType,
                        assignmentEnforcement: _form.assignmentEnforcement
                    };

                    function callBack(data) {
                        if (data.resultCode === "S") {
                            me.$notify({
                                title: '消息',
                                message: '保存成功!',
                                type: 'success'
                            });
                        } else {
                            me.$notify.error({
                                title: '保存出错！',
                                message: data.resultMsg
                            });
                        }
                    };
                    me.ajaxGetRequest(param, callBack);
                } else {
                    console.log('error submit!!');
            return false;
        }
        });
        },
        clearWorkCenter(){
            this.workCenterForm.handle = '';
            this.workCenterForm.plant = '';
            this.workCenterForm.workCenter = '';
            this.workCenterForm.description = '';
            this.workCenterForm.statusFk = '';
            this.workCenterForm.wcCategory = '';
            this.workCenterForm.wcType = ''
            this.workCenterForm.assignmentEnforcement = ''
            this.workCenterForm.canBeReleasedTo = ''
            $("#workCenterHandleHidden").val();
            this.resourceOrWorkcenterData = [];
        },
        deleteWorkCenter(){
            var me = this;
            var hiddenHandle = $("#workCenterHandleHidden").val();
            if (hiddenHandle === "") {
                this.$notify({
                    title: '警告',
                    message: '删除的唯一标识未获取到,请先执行查询!',
                    type: 'warning'
                });
            } else {
                var param = {
                    serviceName: "exposeWorkCenterService",
                    serviceMethodName: "deleteByPrimaryKey",
                    handle: hiddenHandle
                };

                function callBack(data) {
                    if (data.resultCode === "S") {
                        me.workCenterForm = {};
                        me.$notify({
                            title: '消息',
                            message: '删除成功!',
                            type: 'success'
                        });
                    } else {
                        me.$notify.error({
                            title: '删除出错！',
                            message: data.resultMsg
                        });
                    }
                };
                me.ajaxGetRequest(param, callBack);
            }
        },
        ajaxPostRequest(params, callBackFun)
        {
            var me = this, _url = "http://localhost:8083/route";
            axios({
                method: 'post',
                url: _url,
                params: params
            }).then(function (response) {
                if (response.status == 200) {
                    callBackFun(response.data);
                } else {
                    console.log(response);
                }
            }).catch(function (err) {
                console.log(err);
            });
        },
        ajaxGetRequest(params, callBackFun){
            var me = this, _url = "http://localhost:8083/route";
            axios.get(_url, {
                params: params
            }).then(function (response) {
                if (response.status == 200) {
                    callBackFun(response.data);
                } else {
                    console.log(response);
                }
            }).catch(function (err) {
                console.log(err);
            });
        },
        selectedRowWorkCenter(row, event) {
            var handle = row.handle;
            var workCenter = row.workCenter;
            this.workCenterSearchDialog = false;
            this.workCenterForm.workCenter = workCenter;
            $("#workCenterHandleHidden").val(handle);
        },
        selectedRowWorOrRes(row, event) {
            var handle = row.handle;
            var name = row.name;
            this.workOrResDialog = false;
            this.resourceWorkCenterForm.name = name;
            $("#resourceOrWorkcenterFk").val(handle);
        },
        saveOneWorkAndRes(){
            var me = this, _form = me.resourceWorkCenterForm;
            var _workCenterFk = $("#workCenterHandleHidden").val();
            var _resourceOrWorkcenterFk = $("#resourceOrWorkcenterFk").val();
            var _type = _form.type;
            var param = {
                serviceName: "exposeWorkCenterService",
                serviceMethodName: "addResourceRefWork",
                sequence: _form.sequence,
                resourceOrWorkcenterFk: _resourceOrWorkcenterFk,
                workCenterFk: _workCenterFk,
                type: _type
            };

            function callBack(data) {
                if (data.resultCode === "S") {
                    me.resourceOrWorkcenterData = data.result;
                    me.newAddDataDialog = false;
                } else {
                    me.$notify.error({
                        title: '保存出错！',
                        message: data.resultMsg
                    });
                }
            };
            me.ajaxGetRequest(param, callBack);
        },
        deleteOneResRefWork(){
            var me = this;
            var _deleteList = me.selectionList;
            var _workCenterHandle = $("#workCenterHandleHidden").val();
            if (_deleteList.length > 0) {
                var _arryList = [];
                for (var i = 0; i < _deleteList.length; i++) {
                    var obj = _deleteList[i];
                    _arryList.push(obj.handle);
                }
                var param = {
                    serviceName: "exposeWorkCenterService",
                    serviceMethodName: "deleteWorkRefResource",
                    handleData: JSON.stringify(_arryList),
                    workCenterHandle: _workCenterHandle
                };

                function callBack(data) {
                    if (data.resultCode === "S") {
                        me.resourceOrWorkcenterData = data.result;
                    } else {
                        me.$notify.error({
                            title: '删除出错！',
                            message: data.resultMsg
                        });
                    }
                };
                me.ajaxGetRequest(param, callBack);
            } else {
                this.$notify.info({
                    title: '消息',
                    message: '请先选择要删除的数据'
                });
            }
        },
        selectDeleteData(selection){
            this.selectionList = selection;
        }
    }
})